package com.yimi.appstore.adapter;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yimi.appstore.R;
import com.yimi.appstore.activity.AppDetailActivity;
import com.yimi.appstore.anim.Add2CartAnimProvider;
import com.yimi.appstore.download2.DownloadManager;
import com.yimi.appstore.util.GlobalConsts;

/**
 * 首页推荐应用适配器
 * 
 * @author Tank
 * 
 */
public class AppAdapter extends MyAdapter {
	private Add2CartAnimProvider mAdd2CartAnimation;
	private ViewGroup vg;

	public AppAdapter(Context context, List<HashMap<String, Object>> listData) {
		super(context, listData);

	}

	public AppAdapter(Context context, List<HashMap<String, Object>> listData, ViewGroup vg) {
		super(context, listData);
		this.vg = vg;
		mAdd2CartAnimation = new Add2CartAnimProvider(vg);
	}

	class ViewHolder {
		ImageView ivIcon;
		TextView tvName, tvInfo;
		Button button;
		RatingBar rbStars;
		View view;
	}

	@Override
	public View getView(final int position, final View convertView, ViewGroup parent) {
		final HashMap<String, Object> item = listData.get(position);
		View v = showLoadingItem(item);
		if (v != null) {
			return v;
		}
		v = convertView;
		ViewHolder vh = null;
		if (v == null || v.getTag() == null) {
			v = lInflater.inflate(R.layout.listview_app_item, null);
			vh = new ViewHolder();
			vh.ivIcon = (ImageView) v.findViewById(R.id.app_cover);
			vh.tvName = (TextView) v.findViewById(R.id.app_title);
			vh.tvInfo = (TextView) v.findViewById(R.id.app_info);
			vh.button = (Button) v.findViewById(R.id.download_btn);
			vh.view = (RelativeLayout) v.findViewById(R.id.app_layout);
			v.setTag(vh);
		} else {
			vh = (ViewHolder) v.getTag();
		}
		/*
		 * if (item.containsKey(ICON)) { vh.ivIcon.setImageBitmap((Bitmap)
		 * item.get(ICON)); } else {
		 * vh.ivIcon.setImageResource(R.drawable.app_deafault); } if
		 * (item.containsKey(NAME)) {
		 * vh.tvName.setText(item.get(NAME).toString()); } if
		 * (item.containsKey(RECOMMEND_EXPONENT)) {
		 * vh.rbStars.setRating(((Integer) (item.get(RECOMMEND_EXPONENT)) / 2
		 * )); }
		 */
		vh.button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String url = "";
				String appName = "";
				if (position % 9 == 0) {
					url = "http://cdn.market.hiapk.com/data/upload/2012/12_05/12/com.evernote_124926.apk";
					appName = "印象笔记";
				} else if (position % 9 == 1) {
					url = "http://cdn.market.hiapk.com/data/upload/2012/12_04/18/vStudio.Android.Camera360_185136.apk";
					appName = "Camera360";
				}else if (position % 9 == 2){
					url = "http://cdn.market.hiapk.com/data/upload/2012/12_05/15/com.cogsoul.hotwheel_152733.apk";
					appName = "公路赛车";
				}else if (position % 9 == 3){
					url = "http://dl.myappdl.com/Heroes_of_Order_Chaos_1.0.7_uy7.xpk";
					appName = "混沌与秩序之英雄战歌安卓中文版";
				}else if (position % 9 == 4){
					url = "http://img.yingyonghui.com/apk/487068/com.ezone.TeamAwesome.1354252649550.apk";
					appName = "超炫别动队";
				}else if (position % 9 == 5){
					url = "http://g.apk.anzhi.com/apk/201212/06/com.imodjoy7_10147300_0.apk";
					appName = "异界风月传";
				}else if (position % 9 == 6){
					url = "http://cdn.market.hiapk.com/data/upload//2012/12_06/17/com.mpisoft.spymissions_173043.apk";
					appName = "叉车挑战赛";
				}else if (position % 9 == 7){
					url = "http://cdn.market.hiapk.com/data/upload//2012/12_06/16/com.noodlecake.leagueofevilfree_165902.apk";
					appName = "恶魔联盟汉化版";
				}
				DownloadManager downloadManager = DownloadManager.getInstance(context);
				downloadManager.addDownloadTask(url,appName , "" ,GlobalConsts.MEDIA_MUSIC);

			}
		});

		vh.view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				AppDetailActivity.lanuchActivity(context, "");
//				PopupWindow p;
//				Log.d("", "position:" + position);
//				if (position != listData.size() - 1) {
//					p = getpopWindow(context, v, R.drawable.mzw_manager_update_desc_background);
//				} else {
//					p = getpopWindow(context, v, R.drawable.action_view_up_bg);
//				}
//
//				p.showAsDropDown(v);
			}

		});
		return v;
	}

	private PopupWindow getpopWindow(Context cx, View parent, int resId) {
		View contenView = ((Activity) cx).getLayoutInflater().inflate(R.layout.pop_layout, null);
		contenView.setBackgroundResource(resId);
		PopupWindow p = new PopupWindow(contenView, parent.getWidth(), parent.getHeight() * 2 / 3);
		p.setContentView(contenView);
		p.setOutsideTouchable(true);
		p.setBackgroundDrawable(new BitmapDrawable());
		p.setFocusable(true);
		return p;
	};

	/**
	 * 下载app
	 * 
	 * @param context
	 * @param url
	 */
	public void downloadApp(String url, Context context) {

	}
}
